package one.stand.zuul.config;

import brave.Tracing;
import com.jmp.autocofiguration.CacheManagerConfig;
import com.jmp.autocofiguration.JmpProperties;
import one.stand.util.RedisUtil;
import one.stand.zuul.filter.AccessFilter;
import one.stand.zuul.filter.ResponseFilter;
import one.stand.zuul.util.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class OneZuulAutoConfiguration {

    @Autowired
    private JmpProperties jmpProperties;

    @Bean
    public RedisUtil redisUtil() {
        return new RedisUtil();
    }

    @Bean
    public AccessFilter accessFilter(StringRedisTemplate stringRedisTemplate) {
        return new AccessFilter(RateLimiter.create(stringRedisTemplate, 1000L));// for test
    }

    @Bean
    public ErrorReporter errorReporter() {
        return new ErrorReporter();
    }

    @Bean
    public ResponseFilter responseFilter(Tracing tracing) {
        return new ResponseFilter(jmpProperties.isDev() ? null : errorReporter(), tracing);
    }
}
